<?php

class ControllerFeedLatestProductsRSS extends Controller {

    public function index() {
	if ($this->config->get('latest_products_rss_status')) {
	    $output = '<?xml version="1.0" encoding="UTF-8" ?>';
	    $output .= '<rss version="2.0">';
	    $output .= '<channel>';
	    $output .= '<title>' . $this->config->get('config_name') . '</title>';
	    $output .= '<description>' . $this->config->get('config_meta_description') . '</description>';
	    $output .= '<link>' . HTTP_SERVER . '</link>';

	    $this->load->model('catalog/product');
	    $this->load->model('localisation/currency');
 	    
	    $limit = $this->config->get('latest_products_rss_limit') ? $this->config->get('latest_products_rss_limit') : 100;
	    
	    $products = $this->model_catalog_product->getLatestProducts($limit);

	    if (isset($this->request->get['currency'])) {
		$currency = $this->request->get['currency'];
	    }
	    else {
		$currency = $this->currency->getCode();
	    }

	    foreach ($products as $product) {
		if ($product['description']) {
		    $output .= '<item>';
		    $output .= '<title>' . html_entity_decode($product['name'], ENT_QUOTES, 'UTF-8') . '</title>';
		    $output .= '<link>' . HTTP_SERVER . 'index.php?route=product/product&amp;product_id=' . $product['product_id'] . '</link>';

		    $special = $this->model_catalog_product->getProductSpecial($product['product_id']);

		    if ($special) {
			$price = $this->currency->format($this->tax->calculate($special, $product['tax_class_id']), $currency, FALSE, TRUE);
		    } else {
			$price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id']), $currency, FALSE, TRUE);
		    }

		    $p = htmlspecialchars('<p><strong>Price: ' . $price . '</strong></p>');

		    $output .= '<description>' . $p . $product['description'] . '</description>';
		    
		    $output .= '</item>';
		}
	    }

	    $output .= '</channel>';
	    $output .= '</rss>';

	    $this->response->addHeader('Content-Type: application/rss+xml');
	    $this->response->setOutput($output, 0);
	}
    }
}

?>